package com.codeplay.service.impl;

import com.codeplay.entity.Permission;
import com.codeplay.repository.PermissionRepository;
import com.codeplay.service.PermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

@Service
@Transactional
public class PermissionServiceImpl implements PermissionService {

    @Autowired
    private PermissionRepository permissionRepository;

    @Override
    public Permission createPermission(Permission permission) {
        if (permissionRepository.existsByName(permission.getName())) {
            throw new RuntimeException("权限名已存在");
        }
        return permissionRepository.save(permission);
    }

    @Override
    public Permission updatePermission(Long id, Permission permissionDetails) {
        Permission permission = getPermissionById(id);
        
        if (!permission.getName().equals(permissionDetails.getName()) 
            && permissionRepository.existsByName(permissionDetails.getName())) {
            throw new RuntimeException("权限名已存在");
        }

        permission.setName(permissionDetails.getName());
        permission.setDescription(permissionDetails.getDescription());
        permission.setCategory(permissionDetails.getCategory());

        return permissionRepository.save(permission);
    }

    @Override
    public void deletePermission(Long id) {
        if (!permissionRepository.existsById(id)) {
            throw new RuntimeException("权限不存在");
        }
        permissionRepository.deleteById(id);
    }

    @Override
    public Permission getPermissionById(Long id) {
        return permissionRepository.findById(id)
                .orElseThrow(() -> new RuntimeException("权限不存在"));
    }

    @Override
    public Permission getPermissionByName(String name) {
        return permissionRepository.findByName(name)
                .orElseThrow(() -> new RuntimeException("权限不存在"));
    }

    @Override
    public List<Permission> getAllPermissions() {
        return permissionRepository.findAll();
    }

    @Override
    public List<Permission> createPermissions(List<Permission> permissions) {
        // 检查权限名是否重复
        Set<String> newPermissionNames = permissions.stream()
                .map(Permission::getName)
                .collect(Collectors.toSet());
                
        if (newPermissionNames.size() != permissions.size()) {
            throw new RuntimeException("权限列表中存在重复的权限名");
        }
        
        // 检查是否与数据库中的权限名重复
        Set<Permission> existingPermissions = permissionRepository
                .findByNameIn(newPermissionNames);
        if (!existingPermissions.isEmpty()) {
            String duplicateNames = existingPermissions.stream()
                    .map(Permission::getName)
                    .collect(Collectors.joining(", "));
            throw new RuntimeException("以下权限名已存在: " + duplicateNames);
        }
        
        return permissionRepository.saveAll(permissions);
    }

    @Override
    public Set<Permission> getPermissionsByNames(Set<String> names) {
        return permissionRepository.findByNameIn(names);
    }

    @Override
    public boolean existsByName(String name) {
        return permissionRepository.existsByName(name);
    }

    @Override
    public List<Permission> getPermissionsByCategory(String category) {
        return permissionRepository.findByCategory(category);
    }

    @Override
    public List<String> getAllCategories() {
        return permissionRepository.findAllCategories();
    }
} 